package cn.kxlove.zhbj.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import cn.kxlove.zhbj.MainActivity;
import cn.kxlove.zhbj.R;
import cn.kxlove.zhbj.domain.respone.NewsMenuBean;
import cn.kxlove.zhbj.pager.impl.NewsCenterPager;

/**
 * @author kxlove 16/1/27 18:33
 */
public class LeftMenuFragment extends BaseFragment {

    private List<NewsMenuBean.NewsMenuData> mMenuData;

    private int mCurrentItem = 0;

    private LeftMenuAdapter mAdapter;

    @ViewInject(R.id.lv_list)
    private ListView lvList;

    @Override
    protected View initView() {
        View view = View.inflate(mActivity, R.layout.fragment_left_menu, null);
        x.view().inject(this,view);
        return view;
    }

    public void setmMenuData(List<NewsMenuBean.NewsMenuData> mMenuData) {
        mCurrentItem = 0;
        this.mMenuData = mMenuData;

        // 给listview设置数据
        mAdapter = new LeftMenuAdapter();
        lvList.setAdapter(mAdapter);

        lvList.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                mCurrentItem = position;
                mAdapter.notifyDataSetChanged();// 刷新listview

                setCurrentDetailPager(position);

                //收起侧边栏
                toggle();
            }
        });
    }

    class LeftMenuAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mMenuData.size();
        }

        @Override
        public Object getItem(int position) {
            return mMenuData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = View.inflate(mActivity, R.layout.list_item_left_menu,
                    null);
            TextView tvMenu = (TextView) view.findViewById(R.id.tv_menu);
            NewsMenuBean.NewsMenuData info = (NewsMenuBean.NewsMenuData) getItem(position);
            tvMenu.setText(info.title);

            if (position == mCurrentItem) {
                tvMenu.setEnabled(true);// 可用, 变为红色
            } else {
                tvMenu.setEnabled(false);// 不可用, 变为白色
            }

            return view;
        }
    }

    /**
     * 打开或关闭侧边栏
     * 如果当前是打开的状态, 就会关闭侧边栏;如果是关闭状态,就打开侧边栏
     */
    public void toggle() {
        MainActivity mainUI = (MainActivity) mActivity;
        SlidingMenu slidingMenu = mainUI.getSlidingMenu();
        slidingMenu.toggle();//如果当前是打开的状态, 就会关闭侧边栏;如果是关闭状态,就打开侧边栏
    }

    /**
     * 设置当前菜单详情页
     *
     * @param position
     */
    protected void setCurrentDetailPager(int position) {
        // 获取新闻中心的对象 NewsCenterPager
        // 1. 先获取MainActivity
        // 2. 通过MainActivity获取ContentFragment
        // 3. 通过ContentFragment获取NewsCenterPager
        // 4. 调用NewsCenterPager的方法来更新菜单详情页
        MainActivity mainUI = (MainActivity) mActivity;
        ContentFragment contentFragment = mainUI.getContentFragment();
        NewsCenterPager newsCenterPager = contentFragment.getNewsCenterPager();
        newsCenterPager.setCurrentMenuDetailPager(position);
    }
}
